﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Events.Controls
{
    public class EventListControl : UserControl
    {
        protected Repeater rpt;

        private EventRepository _repository;

        private EventRepository Repository
        {
            get
            {
                if (_repository == null)
                    _repository = new EventRepository(SPContext.Current.Web);
                return _repository;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var events = Repository.GetApprovedEvents();            
            rpt.DataSource = events;
            rpt.DataBind();
        }

        protected string GetEndDate(object dtoObject)
        {
            var dto = dtoObject as EventDto;
            if (dto == null)
                return string.Empty;

            //if (dto.StartTime.Date == dto.EndTime.Date)
            //    return string.Empty;

            return "- " + dto.EndTime.ToShortTimeString();
        }

        protected object Iif(bool condition, object trueValue, object falseValue)
        {
            return (condition ? trueValue : falseValue);
        }

        protected string GetAttendeesCount(int eventId)
        {
            var attendees = Repository.GetEventAttendees(eventId);
            return attendees.Count.ToString();
        }
    }
}